package com.CuandoLlegaAndroid;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.AnimationDrawable;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import com.CuandoLlegaAndroid.R;
import com.CuandoLlegaAndroid.Custom.CustomDialog;
import com.CuandoLlegaAndroid.Custom.CustomProgress;
import com.CuandoLlegaAndroid.Custom.CustomToast;
import com.CuandoLlegaAndroid.DDLModels.BusLine;
import com.CuandoLlegaAndroid.DDLModels.Favourite;
import com.CuandoLlegaAndroid.DDLModels.Street;
import com.CuandoLlegaAndroid.Helpers.*;

public class MainConsult extends Activity {

	private BusLine _Line;
	private Street _Street;
	private Street _Intersect;
	private String _Nro;
	private String _Result;
	private String _html;
	private ArrayList<Street> _listStreet;
	private ArrayList<Street> _listIntersect;
	private ArrayList<Street> _listStop;

	private BusLine _lastBusLine;
	private Street _lastStreet;
	private Street _lastIntersect;
	private String _lastNumber;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainconsult);

		this.InitializeLastConfiguration();

		LoadLineaSpinner();
		InitializeControls();
		
		ProcessExtras();
	}
	
	private void ProcessExtras() {

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			String favID = extras.getString("fromShorcut");
			if (favID != null) 
			{
				DataHelper dh = new DataHelper(MainConsult.this);
				ArrayList<Favourite> favorites = dh.selectFavourite(favID);
				final Favourite fav = favorites.get(0);

				if(fav != null)
				{
					final CustomProgress progress = new CustomProgress(this);
					progress.ShowProgress(this.getString(R.string.cargando));
					
					final Handler handler = new Handler() {
						public void handleMessage(Message msg) 
						{
							progress.DismissProgress();
							ShowResult(_Result, fav.Name, fav.Nro, fav.Calle, fav.Inters);
						}
					};
					Thread checkUpdate = new Thread() {  
						public void run() {

							_Result = HTMLHelper.GetWhenArrive(fav.Name, fav.Nro, null, null, null);
							handler.sendEmptyMessage(0);
						}
					};
					checkUpdate.start();
					
				}
			}
		}
	}

	@Override
	public Object onRetainNonConfigurationInstance()
	{
		Spinner _SpinnerLine = (Spinner) findViewById(R.id.ddlLinea);
		final BusLine bs = (BusLine)_SpinnerLine.getSelectedItem();

		Spinner _SpinnerStreet = (Spinner) findViewById(R.id.ddlCalle);
		final Street ss = (Street)_SpinnerStreet.getSelectedItem();

		Spinner _SpinnerInter = (Spinner) findViewById(R.id.ddlInterseccion);
		final Street si = (Street)_SpinnerInter.getSelectedItem();

		EditText _txtNro = (EditText) findViewById(R.id.txtNro);
		_Nro = _txtNro.getText().toString();

		ArrayList<Object> params = new ArrayList<Object>();

		params.add(bs);
		params.add(ss);
		params.add(si);
		params.add(_Nro);

		return params;
	}

	private void InitializeLastConfiguration()
	{
		Object o = this.getLastNonConfigurationInstance();
		if(o!=null)
		{
			ArrayList<Object> params = (ArrayList<Object>)o;

			if(params!=null)
			{

				if(params.get(0)!=null)
					_lastBusLine = (BusLine)params.get(0);

				if(params.get(1)!=null)
					_lastStreet = (Street)params.get(1);

				if(params.get(2)!=null)
					_lastIntersect = (Street)params.get(2);

				if(params.get(3)!=null)
					_lastNumber = (String)params.get(3);
			}
		}
	}

	private void InitializeControls() {

		Button btnConsultar = (Button) findViewById(R.id.btnSubmit);
		Button btnAdd = (Button) findViewById(R.id.btnAdd);
		Button btnTracert = (Button) findViewById(R.id.btnTracert);
		EditText _txtNro = (EditText) findViewById(R.id.txtNro);

		Spinner _SpinnerLine = (Spinner) findViewById(R.id.ddlLinea);
		Spinner _SpinnerStreet = (Spinner) findViewById(R.id.ddlCalle);
		Spinner _SpinnerIntersect = (Spinner) findViewById(R.id.ddlInterseccion);

		_txtNro.setOnKeyListener(new View.OnKeyListener(){

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {

				if(event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU){

					EditText nroText = (EditText)v;
					String text = nroText.getText().toString();
					MainConsult.this.getIntent().putExtra("nro_Key", text);
				}
				return false;
			}

		});

		btnConsultar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(chkNetStatus()){
					Submit();
				}
			}
		});

		btnAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Add();
			}
		});
		
		btnTracert.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ShowTracert();
			}
		});

		_SpinnerLine.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
			{
				_Line =  (BusLine)parent.getSelectedItem();

				MainConsult.this.getIntent().putExtra("linea_Key", _Line.Name);
				if(chkNetStatus())
				{
					FillStreet(parent);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		_SpinnerStreet.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
			{	
				_Street =  (Street)parent.getSelectedItem();

				if(chkNetStatus()){
					FillIntersect(parent);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});


		_SpinnerIntersect.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
			{
				_Intersect =  (Street)parent.getSelectedItem();

				if(chkNetStatus()){
					GetNroParada(parent);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

	public void ShowToast(Context c, String msgText, String l)
	{
		CustomToast toast = new CustomToast(c);
		toast.ShowToast(msgText+" " + l);
	}    


	private void FillStreet(AdapterView<?> parent)
	{
		Spinner _SpinnerStreet = (Spinner) findViewById(R.id.ddlCalle);
		Object item = parent.getSelectedItem();

		String msgText = this.getString(R.string.ttLoadingStreet);

		this.FillAttachedSpinner(item, msgText, 1, _SpinnerStreet, null, _lastStreet);
		_lastStreet = null;

	}



	private void FillIntersect(AdapterView<?> parent)
	{
		Spinner _SpinnerIntersect = (Spinner) findViewById(R.id.ddlInterseccion);
		Spinner _SpinnerLine = (Spinner) findViewById(R.id.ddlLinea);
		Object item = parent.getSelectedItem();

		String msgText = this.getString(R.string.ttLoadingIntersect);

		this.FillAttachedSpinner(item, msgText, 2, _SpinnerIntersect, _SpinnerLine, _lastIntersect);
		_lastIntersect = null;

	}



	private void GetNroParada(AdapterView<?> parent)
	{
		if(_lastNumber !=null)
		{
			EditText _txtNro = (EditText) findViewById(R.id.txtNro);
			_txtNro.setText(_lastNumber);
			MainConsult.this.getIntent().putExtra("nro_Key", _lastNumber);
			_lastNumber = null;
			return;
		}

		Spinner _SpinnerLine = (Spinner) findViewById(R.id.ddlLinea);
		Spinner _SpinnerStreet = (Spinner) findViewById(R.id.ddlCalle);

		Object item = parent.getSelectedItem();

		String msgText = this.getString(R.string.ttLoadingNumber);

		this.FillTxtNumber(item, _SpinnerLine, _SpinnerStreet, msgText);

	}

	public void FillAttachedSpinner(Object item, String msgText, int mode, final Spinner spAttached, Spinner spBack, final Object lastSelection)
	{
		_html=null;

		if(mode==1){

			final BusLine bl = (BusLine)item;

			if(bl.ID=="")
			{
				SpinnerHelper.CleanSnniper(spAttached, MainConsult.this);
			}else{

				final CustomProgress progress = new CustomProgress(this);
				progress.ShowProgress(msgText);

				final Handler handler = new Handler() {
					public void handleMessage(Message msg) 
					{
						progress.DismissProgress();

						ImageView imgButton = (ImageView) findViewById(R.id.btnRefreshStreet);

						if(_listStreet!=null && _listStreet.size()>0)
						{
							imgButton.setImageDrawable(MainConsult.this.getResources().getDrawable(R.drawable.incache));

							SpinnerHelper.LoadSnniper(_listStreet, spAttached, MainConsult.this, lastSelection);
						}
						else{

							imgButton.setImageDrawable(MainConsult.this.getResources().getDrawable(R.drawable.outcache));

							if(_html!=null)
							{
								SpinnerHelper.LoadSnniper(_html, bl.ID, bl.Name, null, null, spAttached, MainConsult.this, lastSelection);
							}
						}
					}
				};
				Thread checkUpdate = new Thread() {  
					public void run() {

						DataHelper dh = new DataHelper(MainConsult.this);
						_listStreet = null;
						_listStreet = dh.selectStreetLine(bl.ID);


						if(_listStreet==null || _listStreet.size()==0){
							_html = HTMLHelper.GetPost("http://www.etr.gov.ar/getData.php?accion=getCalle&idLinea="+bl.ID);
						}

						handler.sendEmptyMessage(0);
					}
				};
				checkUpdate.start();

			}
		}

		if(mode==2){

			final BusLine bl = (BusLine)spBack.getSelectedItem();
			final Street s = (Street)item;
			if(bl.ID=="" || s.ID=="")
			{
				SpinnerHelper.CleanSnniper(spAttached, MainConsult.this);	
			}
			else{

				final CustomProgress progress = new CustomProgress(this);
				progress.ShowProgress(msgText);

				final Handler handler = new Handler() {
					public void handleMessage(Message msg) 
					{
						progress.DismissProgress();

						ImageView imgButton = (ImageView) findViewById(R.id.btnRefreshIntersect);

						if(_listIntersect!=null && _listIntersect.size()>0)
						{
							imgButton.setImageDrawable(MainConsult.this.getResources().getDrawable(R.drawable.incache));

							SpinnerHelper.LoadSnniper(_listIntersect, spAttached, MainConsult.this, lastSelection);
						}
						else{

							imgButton.setImageDrawable(MainConsult.this.getResources().getDrawable(R.drawable.outcache));

							if(_html!=null)
							{    			    	
								SpinnerHelper.LoadSnniper(_html, bl.ID, bl.Name, s.ID, s.Name, spAttached, MainConsult.this, lastSelection);
							}
						}
					}
				};
				Thread checkUpdate = new Thread() {  
					public void run() {

						DataHelper dh = new DataHelper(MainConsult.this);
						_listIntersect = null;
						_listIntersect = dh.selectIntersectStreetLine(bl.ID, s.ID);


						if(_listIntersect==null || _listIntersect.size()==0){
							_html = HTMLHelper.GetPost("http://www.etr.gov.ar/getData.php?accion=getInterseccion&idLinea="+bl.ID+"&idCalle="+s.ID);
						}
						handler.sendEmptyMessage(0);
					}
				};
				checkUpdate.start();
			}
		}
	}



	public void FillTxtNumber(Object item, Spinner spAttached, Spinner spBack, String msgText)
	{
		final Street i = (Street)item;
		final BusLine bl = (BusLine)spAttached.getSelectedItem();
		final Street s = (Street)spBack.getSelectedItem();

		if(i==null || i.ID=="" ||
				bl==null || bl.ID=="" ||
				s==null || s.ID=="")
		{
			return;
		}
		_html = null;

		final CustomProgress progress = new CustomProgress(this);
		progress.ShowProgress(msgText);

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) 
			{
				progress.DismissProgress();

				String txt1 = null;
				String destino1 = null;
				String txt2 = null;
				String destino2 = null;
				String nroH = null;

				Boolean hasNro = false;

				ImageView imgButton = (ImageView) findViewById(R.id.btnRefreshStop);
				imgButton.setImageDrawable(MainConsult.this.getResources().getDrawable(R.drawable.outcache));
				try {

					if(_listStop!=null && _listStop.size()>0)
					{
						imgButton.setImageDrawable(MainConsult.this.getResources().getDrawable(R.drawable.incache));

						if(_listStop.size()>1)
						{
							txt1 = _listStop.get(0).ID;
							destino1 = _listStop.get(0).Name;
							txt2 = _listStop.get(1).ID;
							destino2 = _listStop.get(1).Name;

						}
						else
						{
							txt1 = _listStop.get(0).ID;

							EditText _txtNro = (EditText) findViewById(R.id.txtNro);
							_txtNro.setText(txt1);
							hasNro = true;
						}
					}
					else {

						if(_html==null || _html =="")
						{
							CustomToast toast = new CustomToast(MainConsult.this);
							toast.ShowToast(MainConsult.this.getString(R.string.noSePudoRecuperarNroParada));
							return;
						}

						ArrayList<String> sa = HTMLHelper.FormatNroParada(_html);

						if(sa.size()==6)
						{
							txt1 = sa.get(2);
							destino1 = sa.get(3);
							txt2 = sa.get(4);
							destino2 = sa.get(5);
						}
						else{
							Pattern intsOnly = Pattern.compile(">\\d+.<");
							Matcher makeMatch = intsOnly.matcher(_html);

							if(makeMatch.find()){

								String mnroH = makeMatch.group();
								EditText _txtNro = (EditText) findViewById(R.id.txtNro);

								nroH = mnroH.substring(1, mnroH.length()-1);

								_txtNro.setText(nroH);

								hasNro = true;
							}
						}
					}

					if(!hasNro){
						LayoutInflater inflater = (LayoutInflater) MainConsult.this.getSystemService(LAYOUT_INFLATER_SERVICE);

						final View layout = inflater.inflate(R.layout.stopview,
								(ViewGroup) findViewById(R.id.layout_root));

						TableRow tr1 = (TableRow)layout.findViewById(R.id.tParada1);
						TableRow tr2 = (TableRow)layout.findViewById(R.id.tParada2);

						final TextView txtNro1 = (TextView) layout.findViewById(R.id.txtNro1);
						TextView txtDestino1 = (TextView) layout.findViewById(R.id.txtDestino1);
						final TextView txtNro2 = (TextView) layout.findViewById(R.id.txtNro2);
						TextView txtDestino2 = (TextView) layout.findViewById(R.id.txtDestino2);

						final EditText edit1 = (EditText) layout.findViewById(R.id.txtNro);

						txtNro1.setText(txt1);
						txtDestino1.setText(destino1);
						txtNro2.setText(txt2);
						txtDestino2.setText(destino2);

						tr1.setClickable(true);
						tr2.setClickable(true);

						tr1.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View v) {
								edit1.setText(txtNro1.getText());
							}

						});

						tr2.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View v) {
								edit1.setText(txtNro2.getText());
							}

						});


						CustomDialog.Builder customBuilder = new
						CustomDialog.Builder(MainConsult.this);
						customBuilder.setTitle(R.string.elijaParada)
						.setContentView(layout)
						.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						})
						.setPositiveButton(R.string.aceptar, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								if(which==DialogInterface.BUTTON_POSITIVE){
									EditText _txtNro = (EditText) findViewById(R.id.txtNro);
									_txtNro.setText(edit1.getText());
									MainConsult.this.getIntent().putExtra("nro_Key", edit1.getText());
								}
								dialog.dismiss();
							}
						});

						customBuilder.create().show();
					}

				} 
				catch (Exception e) 
				{
					Pattern intsOnly = Pattern.compile(">\\d+.<");
					Matcher makeMatch = intsOnly.matcher(_html);

					if(makeMatch.find()){

						String mnroH = makeMatch.group();
						EditText _txtNro = (EditText) findViewById(R.id.txtNro);

						nroH = mnroH.substring(1, mnroH.length()-1);

						_txtNro.setText(nroH);
						MainConsult.this.getIntent().putExtra("nro_Key", nroH);
					}
				}

				final String ftxt1 = txt1;
				final String fdestino1 = destino1;
				final String ftxt2 = txt2;
				final String fdestino2 = destino2;
				final String fnroH = nroH;

				Thread t = new Thread()
				{
					public void run() {
						ArrayList<Street> listStop = new ArrayList<Street>();

						if(ftxt1!=null &&
								fdestino1!=null &&
								ftxt2!=null &&
								fdestino2!=null){

							listStop.add(new Street(fdestino1, ftxt1));
							listStop.add(new Street(fdestino2, ftxt2));

						}
						else{
							listStop.add(new Street(null, fnroH));
						}


						DataHelper dh = new DataHelper(MainConsult.this);
						dh.insertStops(bl.ID, bl.Name, s.ID, s.Name, i.ID, i.Name, listStop);
					}
				};

				if(_listStop==null || _listStop.size()==0)
				{
					t.start();
				}
			}
		};

		Thread checkUpdate = new Thread() {  
			public void run() {

				DataHelper dh = new DataHelper(MainConsult.this);
				_listStop = null;
				_listStop = dh.selectStops(bl.ID, s.ID, i.ID);

				if(_listStop==null || _listStop.size()==0){
					_html = HTMLHelper.GetPost("http://www.etr.gov.ar/getData.php?accion=getInfoParadas&idLinea="+bl.ID+"&idCalle="+s.ID+"&idInt="+i.ID);
				}

				handler.sendEmptyMessage(0);
			}
		};
		checkUpdate.start();
	}

	private void Submit() 
	{

		final TextView resultText = (TextView)findViewById(R.id.resultTextView);
		final TextView lineTopTextView = (TextView)findViewById(R.id.lineTopTextView);
		final TextView lineBottomTextView = (TextView)findViewById(R.id.lineBottomTextView);

		Spinner _SpinnerLine = (Spinner) findViewById(R.id.ddlLinea);
		final BusLine bs = (BusLine)_SpinnerLine.getSelectedItem();

		Spinner _SpinnerStreet = (Spinner) findViewById(R.id.ddlCalle);
		final Street ss = (Street)_SpinnerStreet.getSelectedItem();

		Spinner _SpinnerInter = (Spinner) findViewById(R.id.ddlInterseccion);
		final Street si = (Street)_SpinnerInter.getSelectedItem();

		EditText _txtNro = (EditText) findViewById(R.id.txtNro);
		_Nro = _txtNro.getText().toString();

		Boolean hasNro = (_Nro.trim().length()!=0);
		Boolean hasLineName = (bs!=null && bs.ID.trim().length()!=0);

		if(hasNro && hasLineName)
		{

			final CustomProgress progress = new CustomProgress(this);
			progress.ShowProgress(this.getString(R.string.cargando));

			final Handler handler = new Handler() {
				public void handleMessage(Message msg) 
				{
					progress.DismissProgress();

					String streetName = "";
					String intersectName = "";
					if(_Street!=null){
						streetName = _Street.Name;
					}
					if(_Intersect!=null){
						intersectName = _Intersect.Name;
					}


					ShowResult(_Result, bs.Name, _Nro, streetName, intersectName);
					lineTopTextView.setVisibility(0);
					lineBottomTextView.setVisibility(0);
					resultText.setText(_Result);
				}
			};
			Thread checkUpdate = new Thread() {  
				public void run() {

					_Result = HTMLHelper.GetWhenArrive(bs.Name, _Nro, bs, si, ss);
					handler.sendEmptyMessage(0);
				}
			};
			checkUpdate.start();

		}
	}

	private void ShowResult(String result1, String name, String nro, String calle, String inters) {


		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

		View layout = inflater.inflate(R.layout.favoriteview,
				(ViewGroup) findViewById(R.id.layout_root));


		ImageView imageView = (ImageView) layout.findViewById(R.id.icon);
		imageView.setVisibility(View.GONE);

		TextView fechaHoraText = (TextView) layout.findViewById(R.id.fechaHoraText);
		fechaHoraText.setVisibility(2);

		TextView lineaText = (TextView) layout.findViewById(R.id.lineaText);

		lineaText.setText(String.format(this.getString(R.string.lineaDosPuntos),name));

		TextView nroParadaText = (TextView) layout.findViewById(R.id.nroParadaText);
		nroParadaText.setText(String.format(this.getString(R.string.nroParadaDosPuntos),nro));

		TextView resultTextViewPopup = (TextView) layout.findViewById(R.id.resultTextViewPopup);
		resultTextViewPopup.setText(result1);

		TextView ct = (TextView)layout.findViewById(R.id.calleText);
		TextView it = (TextView)layout.findViewById(R.id.interseccionText);

		if(inters!=null && inters!="" && inters!=this.getString(R.string.seleccione)){
			it.setText(String.format(this.getString(R.string.interseccionDosPuntos),inters));
		}
		else{
			it.setVisibility(View.GONE);
		}

		if(calle!=null && calle!="" && calle!=this.getString(R.string.seleccione)){
			ct.setText(String.format(this.getString(R.string.calleDosPuntos),calle));
		}
		else{
			ct.setVisibility(View.GONE);
		}

		CustomDialog.Builder customBuilder = new
		CustomDialog.Builder(this);
		customBuilder.setTitle(R.string.resultado)
		.setContentView(layout)
		.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		})
		.setPositiveButton(R.string.agregarAFavoritos, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(which==DialogInterface.BUTTON_POSITIVE){
					Add();  
				}
				dialog.dismiss();
			}
		});

		customBuilder.create().show();

	}

	private void ShowTracert()
	{
		
		if(_Line!=null){
			
			String url = "http://dl.dropbox.com/u/32179266/"+_Line.ID+".kml";
			
			final Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW,
									Uri.parse("geo:0,0?q="+url));
			startActivity(myIntent);
			
		}
	}
	
	private void Add()
	{	
		EditText _txtNro = (EditText) findViewById(R.id.txtNro);
		String nro = _txtNro.getText().toString();
		Boolean hasntNro = (nro.trim().length()==0);
		Boolean hasntLineID = (_Line.ID.trim().length()==0);
		Boolean hasntLineName = (_Line.Name.trim().length()==0);
		Boolean hasntResult = (_Result==null || _Result.trim().length()==0);

		if(_Line == null || hasntLineID || hasntNro || hasntLineName || hasntResult){
			CustomToast toast = new CustomToast(this);
			toast.ShowToast(this.getString(R.string.datosInsuficientes));
			return;
		}

		DataHelper dh = new DataHelper(this);

		String streetName = "";
		String intersectName = "";

		if(_Street!=null && _Street.ID!="")
			streetName = _Street.Name;

		if(_Intersect!=null && _Intersect.ID!="")
			intersectName = _Intersect.Name;

		dh.insertFavourite(_Line.Name, _Line.ID, nro, _Result,streetName, intersectName, "", "");

		CustomToast toast = new CustomToast(this);
		toast.ShowToast(this.getString(R.string.agregado));

	}

	private void LoadLineaSpinner() {
		Spinner _SpinnerLine = (Spinner) findViewById(R.id.ddlLinea);

		ArrayAdapter<BusLine> _AdapterLine = new ArrayAdapter<BusLine>(this,
				android.R.layout.simple_spinner_item);
		_AdapterLine
		.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		_SpinnerLine.setAdapter(_AdapterLine);

		_AdapterLine.add(new BusLine(this.getString(R.string.seleccione), ""));
		_AdapterLine.add(new BusLine("101", "1,2"));
		_AdapterLine.add(new BusLine("102", "3,4"));
		_AdapterLine.add(new BusLine("103", "5,6"));
		_AdapterLine.add(new BusLine("110", "11"));
		_AdapterLine.add(new BusLine("112", "12,13"));
		_AdapterLine.add(new BusLine("113", "14"));
		_AdapterLine.add(new BusLine("115", "15"));
		_AdapterLine.add(new BusLine("116", "16"));
		_AdapterLine.add(new BusLine("120", "17"));
		_AdapterLine.add(new BusLine("121", "18"));
		_AdapterLine.add(new BusLine("122", "19,20"));
		_AdapterLine.add(new BusLine("123", "21"));
		_AdapterLine.add(new BusLine("125", "22"));
		_AdapterLine.add(new BusLine("126", "23,24"));
		_AdapterLine.add(new BusLine("127", "25"));
		_AdapterLine.add(new BusLine("128", "26,27"));
		_AdapterLine.add(new BusLine("129", "28"));
		_AdapterLine.add(new BusLine("130", "29"));
		_AdapterLine.add(new BusLine("131", "30"));
		_AdapterLine.add(new BusLine("132", "31"));
		_AdapterLine.add(new BusLine("133", "32,33"));
		_AdapterLine.add(new BusLine("134", "34"));
		_AdapterLine.add(new BusLine("135", "35"));
		_AdapterLine.add(new BusLine("136", "36"));
		_AdapterLine.add(new BusLine("137", "37"));
		_AdapterLine.add(new BusLine("138", "38,39"));
		_AdapterLine.add(new BusLine("139", "40,41"));
		_AdapterLine.add(new BusLine("140", "43"));
		_AdapterLine.add(new BusLine("141", "44"));
		_AdapterLine.add(new BusLine("142", "45,46,47"));
		_AdapterLine.add(new BusLine("143", "48,49"));
		_AdapterLine.add(new BusLine("144", "50,51"));
		_AdapterLine.add(new BusLine("145", "52,53"));
		_AdapterLine.add(new BusLine("146", "54,55"));
		_AdapterLine.add(new BusLine("153", "56,57,58"));
		_AdapterLine.add(new BusLine("K", "64"));
		_AdapterLine.add(new BusLine("Linea de la Costa", "65"));
		_AdapterLine.add(new BusLine("Ronda del Centro", "66"));

	}


	private boolean chkNetStatus()
	{
		final ConnectivityManager connMgr = (ConnectivityManager)
		this.getSystemService(Context.CONNECTIVITY_SERVICE);

		final android.net.NetworkInfo wifi =
			connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		final android.net.NetworkInfo mobile =
			connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		if( !wifi.isAvailable() && !mobile.isAvailable())
		{

			CustomToast toast = new CustomToast(this);
			toast.ShowToast(this.getString(R.string.noHayConexion));

			return false;
		}
		return true;
	} 
}